package cn.edu.nuaa.aadl2.generatorC.ui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.eclipse.core.resources.IProject;
import org.osate.aadl2.SystemImplementation;

import cn.edu.nuaa.aadl2.AdaGenerator.AdaCodeGeneration;
import cn.edu.nuaa.aadl2.AdaGenerator.workflow.GenerateAda;
import cn.edu.nuaa.aadl2.generatorC.utils.Tools;
import cn.edu.nuaa.aadl2.generatorC.workflow.Generate;

import java.awt.event.ActionListener;
import java.util.Enumeration;


public class Win extends JFrame {
  
	
    private static final long serialVersionUID = 1L;
    int WIDTH = 525;
    int HEIGHT = 581;
    private  JTextField path;
    private ButtonGroup platFormGroap;
    private ButtonGroup standardGroap;
    
    public Win(final SystemImplementation sys,final IProject iProject)
    {
        super();
        this.getContentPane().setLayout(null);
        
        JLabel label = new JLabel("1.\u76EE\u6807\u5E73\u53F0\uFF1A");
        label.setBounds(50, 30, 107, 18);
        getContentPane().add(label);
        
        JLabel label_1 = new JLabel("3.\u4EE3\u7801\u8DEF\u5F84\uFF1A");
        label_1.setBounds(50, 346, 107, 18);
        getContentPane().add(label_1);
        
        path = new JTextField();
        path.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
        path.setText("D:/lwx/signal/");
        path.setBounds(56, 390, 320, 24);
        getContentPane().add(path);
        path.setColumns(10);
       // System.out.println(path);
        
        
        JButton btnNewButton = new JButton("\u6D4F\u89C8");
        btnNewButton.setBounds(400, 389, 71, 27);
        getContentPane().add(btnNewButton);
        btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = chooser.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					if(chooser.getSelectedFile()!=null)
						path.setText(chooser.getSelectedFile().getPath()+"\\");
				}
			}
		});
        
        JButton button = new JButton("\u786E\u5B9A");
        button.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		Enumeration<AbstractButton> e1= platFormGroap.getElements();
        		Enumeration<AbstractButton> e2= standardGroap.getElements();
        		String platForm = null;
        		String standard = null;
        		while(e1.hasMoreElements()) {
        			
        			AbstractButton b=e1.nextElement();
        			if (b.isSelected()) {
        				platForm=b.getText();
        				break;
					}
        		}
        		while(e2.hasMoreElements()) {
        			AbstractButton b=e2.nextElement();
        			if (b.isSelected()) {
        				standard=b.getText();
        				break;
					}
        		}
        		final String s1=platForm;
        		final String s2=standard;
        		final String p=path.getText();
        		new Thread(new Runnable() {
					@Override
					public void run() {
						System.out.println(s2);
						if(s2=="SAFE C")
							Generate.generate(sys,iProject,p,s1);
						else if(s2=="SAFE Ada")
							AdaCodeGeneration.codeGeneration(sys,iProject,p);
						else {
						}
					}
				}).start();
        		Win.this.setVisible(false);
        	}
        });
        button.setBounds(372, 475, 99, 27);
        getContentPane().add(button);
        
        JButton button_1 = new JButton("\u53D6\u6D88");
        button_1.setBounds(257, 475, 99, 27);
        getContentPane().add(button_1);
        button_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Win.this.setVisible(false);
			}
		});
        
        
        
        platFormGroap = new ButtonGroup();
        JRadioButton rdbtnNewRadioButton = new JRadioButton("VxWorks");
        rdbtnNewRadioButton.setBounds(50, 68, 85, 27);
        getContentPane().add(rdbtnNewRadioButton);
        platFormGroap.add(rdbtnNewRadioButton);
        rdbtnNewRadioButton.setSelected(true);
        
        
        JRadioButton rdbtnPosix = new JRadioButton("POSIX");
        rdbtnPosix.setBounds(50, 100, 69, 27);
        getContentPane().add(rdbtnPosix);
        platFormGroap.add(rdbtnPosix);
        
        JRadioButton rdbtnRtimes = new JRadioButton("RTimes");
        rdbtnRtimes.setBounds(50, 132, 77, 27);
        getContentPane().add(rdbtnRtimes);
        platFormGroap.add(rdbtnRtimes);
        
        JRadioButton rdbtnAda = new JRadioButton("Ada\u8FD0\u884C\u65F6");
        rdbtnAda.setBounds(50, 164, 139, 27);
        getContentPane().add(rdbtnAda);
        platFormGroap.add(rdbtnAda);
        
        
     
        
        JLabel label_2 = new JLabel("2.\u5B89\u5168\u7F16\u7801\u89C4\u8303\uFF1A");
        label_2.setBounds(55, 226, 176, 18);
        getContentPane().add(label_2);
        
        
        
        standardGroap = new ButtonGroup();
        
        JRadioButton rdbtnada = new JRadioButton("SAFE C");
        rdbtnada.setBounds(50, 265, 85, 27);
        getContentPane().add(rdbtnada);
        standardGroap.add(rdbtnada);
        rdbtnada.setSelected(true);
        
        JRadioButton rdbtnada_1 = new JRadioButton("SAFE Ada");
        rdbtnada_1.setBounds(50, 297, 85, 27);
        getContentPane().add(rdbtnada_1);
        standardGroap.add(rdbtnada_1);
        
        
        this.setTitle("\u6C42\u7CBE\u5C5E\u6027\u914D\u7F6E");
        centerWindow(this);
        
    }
    
    void centerWindow(Component component)
    {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension scmSize = toolkit.getScreenSize();
        component.setLocation(scmSize.width / 2 - (WIDTH / 2), scmSize.height / 2 - (HEIGHT / 2));
        component.setSize(new Dimension(WIDTH, HEIGHT));
    }
//    public static void main(String[] args) {
//		Win win=new Win(null);
//		win.setVisible(true);
//	}
}
